<script setup lang="ts">
import { ref } from "vue";
import { useRole } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { regionData } from "@/utils/chinaArea";

import Eye from "~icons/ri/Eye-line";
import Refresh from "~icons/ep/refresh";
import Checkbox from "~icons/ri/checkbox-fill";
import Arrowleftright from "~icons/ri/arrow-left-right-line";
import Share from "~icons/ri/share-box-line";

import { Phone, Switch, Position } from "@element-plus/icons-vue";

import CustomerDetail from "@/components/customerDetail/index.vue";

defineOptions({
  name: "myClientDeal"
});

const formRef = ref();
const detailRef = ref();
const releaseRef = ref();
const {
  permissions,

  labelList,
  goodSearchList,
  form,
  loading,
  columns,
  dataList,
  pagination,
  tableRef,
  multipleSelection,

  onSearch,
  resetForm,
  lookCustomer,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleSelectionClear,

  transferClick,
  releaseClick
} = useRole(detailRef, releaseRef);
const phone = ref("")
const Detail = ref({})
const emit = defineEmits(['dialPop','customerData','closePop'])
const handleClick = ( data ) => {
  emit('dialPop', true); // 打开拨打的弹框
  emit('customerData', data) // 获取客户详情
}
// 关闭弹框
const changeClosePop = () => {
  emit('closePop', false)
}
</script>

<template>
  <div class="main">
    <!-- <div @click="handleClick">打开拨打电话框</div> -->
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item prop="keyword">
        <el-input
          v-model="form.keyword"
          placeholder="客户名/企业名/手机号"
          clearable
          class="w-[180px]!"
        />
      </el-form-item>
      <el-form-item prop="tag_id">
        <el-select
          v-model="form.tag_id"
          placeholder="客户标签"
          clearable
          class="w-[120px]!"
        >
          <el-option v-for="(item, index) in labelList" :key="index" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="address">
        <el-cascader
          v-model="form.address"
          placeholder="所在地区"
          :options="regionData"
          :props="{ checkStrictly: true, value: 'label' }"
          clearable
          class="w-[120px]!"
        />
      </el-form-item>
      <el-form-item prop="product_id">
        <el-cascader
          v-model="form.product_id"
          class="w-[220px]!"
          placeholder="成交产品"
          :options="goodSearchList"
          :props="{ value: 'id', label: 'name' }"
          clearable
        />
      </el-form-item>

      <el-form-item v-if="permissions.is_mc_cj_table">
        <el-button
          type="primary"
          :icon="useRenderIcon('ri/search-line')"
          :loading="loading"
          @click="onSearch"
        >
          查询
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar
      title=""
      :columns="columns"
      @refresh="onSearch"
      :key="columns.length"
    >
      <template v-slot="{ size, dynamicColumns }">
        <!-- 多选显示 -->
        <div class="multipleInner" v-if="multipleSelection.length">
          <IconifyIconOffline
            :icon="Checkbox"
            style="color: #fff;width: 18px;height: 18px;cursor: pointer;"
            @click="handleSelectionClear"
          />
          <b>已选中 {{ multipleSelection.length }} 项</b>

          <div class="multipHandle" @click="transferClick" v-if="permissions.is_cd_cj_transfer">
            <IconifyIconOffline :icon="Arrowleftright" />
            <p>转移给他人</p>
          </div>
          <div class="multipHandle" @click="releaseClick" v-if="permissions.is_cd_cj_release">
            <IconifyIconOffline :icon="Share" />
            <p>释放到公海</p>
          </div>
        </div>
        <pure-table
          ref="tableRef"
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(Eye)"
              @click="()=>{
                lookCustomer(row)
                changeClosePop()
                phone = row.phone
                Detail = row
              }"
            >
            </el-button>
          </template>
          <template #Phone="{ row }">
           <el-icon color="#60d1d1" @click="handleClick(row)">
              <Phone />
            </el-icon>
          </template>
        </pure-table>
      </template>
    </PureTableBar>

    <CustomerDetail ref="detailRef">
      <template v-slot>
        <el-button class="handleButton" type="primary" :icon="Phone" @click="handleClick(Detail)">
          拨打电话
        </el-button>
        <el-button
          v-if="permissions.is_cd_cj_transfer"
          class="handleButton"
          type="primary"
          :icon="Switch"
          @click="transferClick"
        >
          转移给他人
        </el-button>
        <el-button
          v-if="permissions.is_cd_cj_release"
          class="handleButton"
          type="primary"
          :icon="Position"
          @click="releaseClick"
        >
          释放到公海
        </el-button>
      </template>
    </CustomerDetail>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-form--inline .el-form-item) {
  margin-right: 12px;
}

.multipleInner {
  background-color: var(--pure-theme-menu-active-before);
  height: 45px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  font-size: 14px;
  color: #ffffff;

  b {
    margin-left: 3px;
    margin-right: 10px;
  }

  .multipHandle {
    padding: 5px 8px;
    margin-left: 10px;
    display: flex;
    align-items: start;
    cursor: pointer;
    border-radius: 5px;
    transition: all 0.1s;

    svg {
      font-size: 16px;
      color: #ffffff;
      margin-top: 2px;
    }

    p {
      margin-left: 5px;
    }

    &:hover {
      background-color: #ffffff;
      color: #000000;

      :deep(svg) {
        color: #000000;
      }
    }
  }
}

.main {
  margin: 12px 12px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}

:deep(.handleButton) {
  margin-top: 30px;
}
</style>
